﻿namespace PrismEx.Fixtures.Steps
{
    using System.ServiceModel;

    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.ServiceLocation;

    using Moq;

    using PrismEx.Distributed.Events;
    using PrismEx.Distributed.Interfaces;
    using PrismEx.Distributed.Internals;
    using PrismEx.Events;

    using TechTalk.SpecFlow;

    [Binding]
    public class DistributedEventSteps
    {
        [Given(@"a service proxy has been created")]
        public void GivenAServiceProxyHasBeenCreated()
        {
            var channelMock = new Mock<IDistributedEventChannel>();
            var channel = channelMock.Object;

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IDistributedEventChannel>()).Returns(channel);
            channelMock.Setup(x => x.GetProperty<IOnlineStatus>()).Returns(new Mock<IOnlineStatus>().Object);
            ServiceLocator.SetLocatorProvider(() => serviceLocatorMock.Object);

            ScenarioContext.Current["ChannelProxy"] = channel;
        }  
        
        [When(@"a distributed event is published")]
        public void WhenADistributedEventIsPublished()
        {
            var @event = new DistributedEventFake();
            var eventAggregator = ScenarioContext.Current["EventAggregator"] as IEventAggregator;

            eventAggregator.Publish(@event);
        }

        [Then(@"the service proxy should have been called")]
        public void ThenTheServiceProxyShouldHaveBeenCalled()
        {
            var channel = ScenarioContext.Current["ChannelProxy"] as IDistributedEventChannel;

            Mock.Get(channel).Verify(x => x.Publish(It.IsAny<EventDto>()));
        }

        private class DistributedEventFake : DistributedEvent<DistributedEventFake>
        {
        }
    }
}